﻿Imports System.IO
Imports System.Data.OleDb

Public Class Frm_Etiket

    Public Shared Degisim_Tarihi As DateTime
    Public _Temp_etk_file_path As String
    Public _Depo_No As String
    Public _Etiket_Yol As String
    Public _Yazdirma_Miktari As Integer = 50
    Public _Database_File As String
    Public exePath As String = ""
    Private GunlukBasildi As Boolean = False
    Private EtiketListesi As List(Of MikroMagazaObject.EtiketSettingsObject)

    Private _fiyatDegisikligiEvragi As Boolean = False
    Public Property FiyatDegisikligiEvragi() As Boolean
        Get
            Return _fiyatDegisikligiEvragi
        End Get
        Set(ByVal value As Boolean)
            _fiyatDegisikligiEvragi = value
        End Set
    End Property


    Sub New()
        InitializeComponent()
        EtiketListesi = MikroMagazaObject.Settings.Get.EtiketAyarlari
        'Database_File = Db_file
        '_Temp_etk_file_path = temp_etiket_dosyasi
        '_Design_File_Path_Ean13 = dizayn_dosyasi_13
        '_Design_File_Path_Ean8 = dizayn_dosyasi_8
        '_Etiket_Yol = etiketyol
        '_Depo_No = depono
    End Sub
    Private Sub GetChangeStockCards2()
        Dim frm As New Frm_Tarih
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterParent
        frm.ShowDialog(Me)

        Dim cn As New OleDbConnection(Cnn_String)
        cn.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = cn
        cmd.CommandText = "SELECT * FROM Q_DegisenEtiketler2"
        cmd.Parameters.Add("@depoid", OleDbType.VarChar)
        cmd.Parameters.Add("@tarih", OleDbType.Date)
        cmd.Parameters.Item("@depoid").Value = _Depo_No
        cmd.Parameters.Item("@tarih").Value = Degisim_Tarihi

        Dim adp As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        cn.Close()

        Dim barkod As String = ""
        Dim stokad As String = ""
        Dim genelfiyat As Decimal = 0
        Dim depofiyat As Decimal = 0
        Dim genelfiyat2 As Decimal = 0
        Dim depofiyat2 As Decimal = 0
        Dim kullanilacakfiyat As Decimal = 0
        Dim kullanilacakfiyat2 As Decimal = 0
        Dim etikettipi As Integer = 0
        Dim birim1ad As String = ""
        Dim agirlik As Decimal = 0

        Dim rw As Integer = 1
        For i As Integer = 0 To dt.Rows.Count - 1
            barkod = dt.Rows(i)("barkod")
            stokad = dt.Rows(i)("stokad")
            If Not dt.Rows(i)("genelfiyat1") Is DBNull.Value Then genelfiyat = dt.Rows(i)("genelfiyat1") Else genelfiyat = 0
            If Not dt.Rows(i)("depofiyat1") Is DBNull.Value Then depofiyat = dt.Rows(i)("depofiyat1") Else depofiyat = 0
            If Not dt.Rows(i)("genelfiyat2") Is DBNull.Value Then genelfiyat2 = dt.Rows(i)("genelfiyat2") Else genelfiyat2 = 0
            If Not dt.Rows(i)("depofiyat2") Is DBNull.Value Then depofiyat2 = dt.Rows(i)("depofiyat2") Else depofiyat2 = 0
            If Not dt.Rows(i)("barkodtipi") Is DBNull.Value Then etikettipi = dt.Rows(i)("barkodtipi") Else etikettipi = 0
            If Not dt.Rows(i)("birim1ad") Is DBNull.Value Then birim1ad = dt.Rows(i)("birim1ad") Else etikettipi = 0
            If Not dt.Rows(i)("birim1agirlik") Is DBNull.Value Then agirlik = dt.Rows(i)("birim1agirlik") Else agirlik = 0

            If depofiyat <= 0 Then
                kullanilacakfiyat = genelfiyat
            Else
                kullanilacakfiyat = depofiyat
            End If
            If depofiyat2 <= 0 Then
                kullanilacakfiyat2 = genelfiyat2
            Else
                kullanilacakfiyat2 = depofiyat2
            End If

            If stokad.StartsWith("MNV") Then
                C1FlexGrid1(rw, 0) = False
            Else
                C1FlexGrid1(rw, 0) = True
            End If

            C1FlexGrid1(rw, 1) = barkod
            C1FlexGrid1(rw, 2) = stokad
            C1FlexGrid1(rw, 3) = kullanilacakfiyat
            C1FlexGrid1(rw, 4) = 1
            C1FlexGrid1(rw, 5) = etikettipi
            C1FlexGrid1(rw, 6) = birim1ad
            C1FlexGrid1(rw, 7) = kullanilacakfiyat2
            C1FlexGrid1(rw, 8) = agirlik

            rw += 1
            C1FlexGrid1.Rows.Add()
        Next
    End Sub

    Private Sub GetChangeStockCards()
        Dim cn As New OleDbConnection(Cnn_String)
        cn.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = cn
        cmd.CommandText = "SELECT * FROM Q_DegisenEtiketler"
        cmd.Parameters.Add("@depoid", OleDbType.VarChar)
        cmd.Parameters.Add("@tarih", OleDbType.Date)
        cmd.Parameters.Item("@depoid").Value = _Depo_No
        cmd.Parameters.Item("@tarih").Value = New DateTime(Now.Year, 1, 1, 0, 0, 0)

        Dim adp As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        cn.Close()

        Dim barkod As String = ""
        Dim stokad As String = ""
        Dim genelfiyat As Decimal = 0
        Dim depofiyat As Decimal = 0
        Dim genelfiyat2 As Decimal = 0
        Dim depofiyat2 As Decimal = 0
        Dim kullanilacakfiyat As Decimal = 0
        Dim kullanilacakfiyat2 As Decimal = 0
        Dim etikettipi As Integer = 0
        Dim birim1ad As String = ""
        Dim agirlik As Decimal = 0

        Dim rw As Integer = 1
        For i As Integer = 0 To dt.Rows.Count - 1
            barkod = dt.Rows(i)("barkod")
            stokad = dt.Rows(i)("stokad")
            If Not dt.Rows(i)("genelfiyat1") Is DBNull.Value Then genelfiyat = dt.Rows(i)("genelfiyat1") Else genelfiyat = 0
            If Not dt.Rows(i)("depofiyat1") Is DBNull.Value Then depofiyat = dt.Rows(i)("depofiyat1") Else depofiyat = 0
            If Not dt.Rows(i)("genelfiyat2") Is DBNull.Value Then genelfiyat2 = dt.Rows(i)("genelfiyat2") Else genelfiyat2 = 0
            If Not dt.Rows(i)("depofiyat2") Is DBNull.Value Then depofiyat2 = dt.Rows(i)("depofiyat2") Else depofiyat2 = 0
            If Not dt.Rows(i)("barkodtipi") Is DBNull.Value Then etikettipi = dt.Rows(i)("barkodtipi") Else etikettipi = 0
            If Not dt.Rows(i)("birim1ad") Is DBNull.Value Then birim1ad = dt.Rows(i)("birim1ad") Else etikettipi = 0
            If Not dt.Rows(i)("birim1agirlik") Is DBNull.Value Then agirlik = dt.Rows(i)("birim1agirlik") Else agirlik = 0

            If depofiyat <= 0 Then
                kullanilacakfiyat = genelfiyat
            Else
                kullanilacakfiyat = depofiyat
            End If

            If depofiyat2 <= 0 Then
                kullanilacakfiyat2 = genelfiyat2
            Else
                kullanilacakfiyat2 = depofiyat2
            End If

            If stokad.StartsWith("MNV") Then
                C1FlexGrid1(rw, 0) = False
            Else
                C1FlexGrid1(rw, 0) = True
            End If

            C1FlexGrid1(rw, 1) = barkod
            C1FlexGrid1(rw, 2) = stokad
            C1FlexGrid1(rw, 3) = kullanilacakfiyat
            C1FlexGrid1(rw, 4) = 1
            C1FlexGrid1(rw, 5) = etikettipi
            C1FlexGrid1(rw, 6) = birim1ad
            C1FlexGrid1(rw, 7) = kullanilacakfiyat2
            C1FlexGrid1(rw, 8) = agirlik

            rw += 1
            C1FlexGrid1.Rows.Add()
        Next
    End Sub

    Private Sub EtiketAdlari()
        cmb_etikettip.Items.Clear()
        For i As Integer = 0 To EtiketListesi.Count - 1
            cmb_etikettip.Items.Add(EtiketListesi(i).Adi)
        Next


    End Sub

    Private Function GetEanDizaynFile(ByVal tip As Integer) As String
        Dim sonuc As String = ""
        For i As Integer = 0 To EtiketListesi.Count - 1
            If cmb_etikettip.Text = EtiketListesi(i).Adi Then
                If tip = 0 Then
                    sonuc = EtiketListesi(i).Ean13
                Else
                    sonuc = EtiketListesi(i).Ean8
                End If
                Exit For
            End If
        Next
        Return sonuc
    End Function

    Private Sub EtiketYolDegistir()
        For i As Integer = 0 To EtiketListesi.Count - 1
            If cmb_etikettip.Text = EtiketListesi(i).Adi Then
                _Etiket_Yol = EtiketListesi(i).Printer
                Exit For
            End If
        Next
    End Sub

    Private Sub Yazdir()
        Try
            If _Yazdirma_Miktari <= 0 Then
                _Yazdirma_Miktari = 50
            End If

            If File.Exists(_Temp_etk_file_path) = True Then
                File.Delete(_Temp_etk_file_path)
            End If
            Dim basim_sayisi As Integer = 0
            Dim toplambaski As Integer = 0
            Dim toplamBasilacak As Integer = CalcRow()
            Dim ports As String = "COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,COM10,LPT1,LPT2,LPT3,LPT4"

            For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
                If C1FlexGrid1(i, 1) <> "" And C1FlexGrid1(i, 2) <> "" And C1FlexGrid1(i, 0) = True And C1FlexGrid1(i, 3) > 0 Then
                    Dim adet As Integer = C1FlexGrid1(i, 4)
                    Dim tip As Integer = C1FlexGrid1(i, 5)
                    C1FlexGrid1(i, 0) = False

                    For a As Integer = 1 To adet
                        Select Case tip
                            Case 0
                                Label_Design_Read(GetEanDizaynFile(0), i, _Temp_etk_file_path)
                            Case 1
                                Label_Design_Read(GetEanDizaynFile(1), i, _Temp_etk_file_path)
                            Case Else
                                Label_Design_Read(GetEanDizaynFile(0), i, _Temp_etk_file_path)
                        End Select
                        basim_sayisi += 1
                        toplambaski += 1

                        If ports.IndexOf(_Etiket_Yol) > -1 Then 'standart port dan gönderilecekse
                            If basim_sayisi = _Yazdirma_Miktari Then
                                If _Etiket_Yol.IndexOf("\") > -1 Then
                                    File.Copy(_Temp_etk_file_path, _Etiket_Yol & "\etiket.etk")
                                Else
                                    File.Copy(_Temp_etk_file_path, _Etiket_Yol & ":")
                                End If
                                If File.Exists(_Temp_etk_file_path) = True Then
                                    File.Delete(_Temp_etk_file_path)
                                End If
                                basim_sayisi = 0

                                Dim uyari As MsgBoxResult = MsgBox("Toplam Basılacak :" & toplamBasilacak & vbCrLf & "Yazdırılan :" & toplambaski & vbCrLf & "Yazdırmaya Devam Etmek için Tamam'a Basın", MsgBoxStyle.YesNo, "")
                                If uyari = MsgBoxResult.Yes Then

                                Else
                                    Exit Sub
                                End If
                            End If
                        Else

                            If cmb_etikettip.Text.IndexOf("A4") > -1 Then
                                MikroMagazaObject.DirectPrint.SendFileToPrinter(_Etiket_Yol, _Temp_etk_file_path)
                            Else
                                m_nFirstCharOnPage = 0
                                Dim rc As New RichTextBoxEx
                                rc.LoadFile(_Temp_etk_file_path)
                                RichQ.Enqueue(rc)
                                PrintDocument1.PrinterSettings.PrinterName = _Etiket_Yol
                                PrintDocument1.Print()
                                If File.Exists(_Temp_etk_file_path) = True Then
                                    File.Delete(_Temp_etk_file_path)
                                End If
                            End If
                        End If
                    Next
                End If
            Next

            'işlem bitince kalanı da gönderip işlemi keser

            If ports.IndexOf(_Etiket_Yol) > -1 Then 'standart port dan gönderilecekse
                If _Etiket_Yol.IndexOf("\") > -1 Then
                    File.Copy(_Temp_etk_file_path, _Etiket_Yol & "\etiket.etk")
                Else
                    File.Copy(_Temp_etk_file_path, _Etiket_Yol & ":")
                End If
                If File.Exists(_Temp_etk_file_path) = True Then
                    File.Delete(_Temp_etk_file_path)
                End If
            End If

            If GunlukBasildi = True Then
                Dim msg As MsgBoxResult = MsgBox("Günlük Alınan Ürünlerin tekrar etiketleri çıkarılmaması için silmek isterminiz ?", MsgBoxStyle.YesNo)
                If msg = MsgBoxResult.Yes Then
                    Dim cn As New OleDbConnection(Cnn_String)
                    cn.Open()
                    Dim cmd As New OleDbCommand
                    cmd.Connection = cn
                    cmd.CommandText = "DELETE FROM TBL_HISTORY"
                    cmd.ExecuteNonQuery()
                    cn.Close()
                End If
            End If
            MsgBox("İşlem Tamamlandı")

        Catch ex As Exception
            MsgBox("Yazıcının Açık Olduğundan Emin Olunuz" & vbCrLf & ex.Message)
        End Try
    End Sub
    Private Function Alan_Deger(ByVal Alan As String, ByVal row As Integer) As String
        Dim deger As String = ""
        Select Case Alan
            Case "BARKODU"
                Return C1FlexGrid1(row, 1)
            Case "STOKADI"
                Return C1FlexGrid1(row, 2)
            Case "FIYAT1"
                Return Format(C1FlexGrid1(row, 3), "#0.00")
            Case "FIYAT2"
                Return Format(C1FlexGrid1(row, 7), "#0.00")
            Case "BIRIMI"
                Return C1FlexGrid1(row, 6)
                'Case "BIRIM ADI"
                '    Return Str_birimadi
                'Case "NET AGIRLIK"
                '    Return Str_netagirlik
            Case "KG FIYATI"
                Dim agirlik As Decimal = C1FlexGrid1(row, 8)
                Dim kgfiyat As Decimal = C1FlexGrid1(row, 3) / agirlik
                deger = Format(kgfiyat, "#0.000")
            Case "TARIH"
                Return Now.ToShortDateString
            Case "SAAT"
                Return Now.ToShortTimeString
                'Case "BRD"
                '    Return ds.Tables(0).Rows(0)(1).ToString
                'Case "OZEL1"
                '    Return Special1.ToString
                'Case "OZEL2"
                '    Return Special2.ToString
                'Case "OZEL3"
                '    Return Special3.ToString
                'Case "OZEL4"
                '    Return Special4.ToString
                'Case "OZEL5"
                '    Return Special5.ToString
                'Case "STOK KOD"
                '    Return ds.Tables(0).Rows(0)(0).ToString
                'Case "BEDEN"
                '    Return ds.Tables(0).Rows(0)(2).ToString
                'Case "BOY"
                '    Return ds.Tables(0).Rows(0)(3).ToString
                'Case "RENK"
                '    Return ds.Tables(0).Rows(0)(4).ToString
        End Select
        Return deger
    End Function

    Private Sub Label_Design_Read(ByVal Lbl_File As String, ByVal row As Integer, ByVal tmpfile As String)
        On Error Resume Next
        If File.Exists(Lbl_File) Then
            Dim sr As StreamReader = New StreamReader(Lbl_File)
            Dim line As String
            Dim ayrac1 As Integer = 0
            Dim Param As String = ""
            Dim Param2 As String = ""
            Dim Alan As String = ""
            Dim uzunluk As Integer = 0

            Dim spalan() As String
            Dim spalan2() As String
            While sr.Peek() > -1
                line = sr.ReadLine()

                ayrac1 = InStr(1, line, "@") 'eğer tanımlı alanlar varsa
                If ayrac1 > 0 Then

                    spalan = line.Split("@")
                    spalan2 = spalan(1).Split(",")
                    Param = spalan(0)
                    Param2 = spalan(2)
                    Alan = spalan2(0)

                    'If Alan <> "FIYAT1" And Alan <> "FIYAT2" Then
                    uzunluk = spalan2(2)
                    'Else
                    '    uzunluk = spalan2(1)
                    'End If

                    Lbl_File_Write(Param & Mid(Alan_Deger(Alan, row), 1, uzunluk) & Param2, tmpfile)
                Else
                    Lbl_File_Write(line, tmpfile)
                End If
            End While

            sr.Close()
        End If
    End Sub
    Private Sub Lbl_File_Write(ByVal Lbl_Text As String, ByVal tmpetk As String)
        Dim sw As New StreamWriter(tmpetk, True, System.Text.Encoding.GetEncoding("ibm857"))
        sw.WriteLine(Lbl_Text)
        sw.Close()
    End Sub

    Private Sub GünlükAlınanlarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GünlükAlınanlarToolStripMenuItem.Click
        GetChangeStockCards()
        CalcRow()
        GunlukBasildi = True
    End Sub

    Private Sub BelirlenenTarihtenSonraDeğişenlerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BelirlenenTarihtenSonraDeğişenlerToolStripMenuItem.Click
        GetChangeStockCards2()
        CalcRow()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYazdir.Click
        Yazdir()
    End Sub

    Private Sub Frm_Etiket_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If FiyatDegisikligiEvragi Then
            btnYazdir.Visible = False
            btnKaydet.Visible = True
            Me.Text = "Fiyat Değişikliği"
        End If

        C1FlexGrid1.Cols(5).Visible = False
        C1FlexGrid2.Cols(4).Visible = False
        C1FlexGrid1.Cols(9).Visible = False
        C1FlexGrid2.Cols(8).Visible = False
        EtiketAdlari()
        cmb_etikettip.SelectedIndex = 0
    End Sub

    Private Sub DışardanDosyaAlımıToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DışardanDosyaAlımıToolStripMenuItem.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dosyaal(OpenFileDialog1.FileName)
            CalcRow()
        End If
    End Sub


    Private Sub StokBul(ByVal aranan As String)
        C1FlexGrid2.Rows.Count = 2
        C1FlexGrid2.Rows(1).Clear(C1.Win.C1FlexGrid.ClearFlags.All)


        If aranan.IndexOf("*") > -1 Then
            aranan = aranan.Replace("*", "%")
        End If
        If cnn.State = ConnectionState.Closed Then
            cnn.ConnectionString = Cnn_String
            cnn.Open()
        End If

        Dim cmd As New OleDbCommand
        cmd.Connection = cnn
        cmd.CommandText = "SELECT * FROM Q_StokAdBul where stokad like'" & aranan & "'"
        cmd.Parameters.Add("@depoid", OleDbType.VarChar)
        cmd.Parameters.Item("@depoid").Value = _Depo_No

        Dim rd As OleDb.OleDbDataReader
        rd = cmd.ExecuteReader



        Dim stokid As Integer = 0
        Dim barkod As String = ""
        Dim stokad As String = ""
        Dim anagrup As String = ""
        Dim genelfiyat As Decimal = 0
        Dim depofiyat As Decimal = 0
        Dim genelfiyat2 As Decimal = 0
        Dim depofiyat2 As Decimal = 0
        Dim kullanilacakfiyat As Decimal = 0
        Dim kullanilacakfiyat2 As Decimal = 0
        Dim etikettipi As Integer = 0
        Dim birim1ad As String = ""
        Dim rw As Integer = C1FlexGrid2.Rows.Count - 1
        Dim tarih As String = ""
        Dim agirlik As Decimal = 0


        If rd.HasRows = True Then
            While rd.Read

                stokid = rd("stokid")
                barkod = rd("barkod")
                stokad = rd("stokad")
                If Not rd("genelfiyat1") Is DBNull.Value Then genelfiyat = rd("genelfiyat1") Else genelfiyat = 0
                If Not rd("depofiyat1") Is DBNull.Value Then depofiyat = rd("depofiyat1") Else depofiyat = 0
                If Not rd("genelfiyat2") Is DBNull.Value Then genelfiyat2 = rd("genelfiyat2") Else genelfiyat2 = 0
                If Not rd("depofiyat2") Is DBNull.Value Then depofiyat2 = rd("depofiyat2") Else depofiyat2 = 0
                If Not rd("barkodtipi") Is DBNull.Value Then etikettipi = rd("barkodtipi") Else etikettipi = 0
                If Not rd("birim1ad") Is DBNull.Value Then birim1ad = rd("birim1ad") Else etikettipi = 0
                If Not rd("tarih") Is DBNull.Value Then tarih = rd("tarih") Else tarih = ""
                If Not rd("birim1agirlik") Is DBNull.Value Then agirlik = rd("birim1agirlik") Else agirlik = 0
                If Not rd("anagrupkod") Is DBNull.Value Then anagrup = rd("anagrupkod") Else anagrup = ""

                If depofiyat <= 0 Then
                    kullanilacakfiyat = genelfiyat
                Else
                    kullanilacakfiyat = depofiyat
                End If
                If depofiyat2 <= 0 Then
                    kullanilacakfiyat2 = genelfiyat2
                Else
                    kullanilacakfiyat2 = depofiyat2
                End If


                C1FlexGrid2(rw, 0) = barkod
                C1FlexGrid2(rw, 1) = stokad
                C1FlexGrid2(rw, 2) = kullanilacakfiyat
                C1FlexGrid2(rw, 6) = kullanilacakfiyat2
                C1FlexGrid2(rw, 3) = birim1ad
                C1FlexGrid2(rw, 4) = etikettipi
                C1FlexGrid2(rw, 5) = tarih
                C1FlexGrid2(rw, 7) = agirlik
                C1FlexGrid2(rw, 8) = stokid
                C1FlexGrid2(rw, 9) = anagrup

                rw += 1
                C1FlexGrid2.Rows.Add()
                C1FlexGrid2.Refresh()

            End While
        Else
            MsgBox(barkod & "  Ürün Bulunamadı !")
        End If
        rd.Close()
        TabControl1.SelectedIndex = 1
    End Sub

    Dim cnn As New OleDbConnection(Cnn_String)
    Private Function Grideyaz(ByVal barkod As String, ByVal adet As Integer, ByVal rw As Integer, Optional ByVal mesajVerme As Boolean = False) As Boolean
        If cnn.State = ConnectionState.Closed Then
            cnn.ConnectionString = Cnn_String
            cnn.Open()
        End If
        Dim cmd As New OleDbCommand
        cmd.Connection = cnn
        cmd.CommandText = "SELECT * FROM Q_StokBul"
        cmd.Parameters.Add("@depoid", OleDbType.VarChar)
        cmd.Parameters.Add("@barkod", OleDbType.VarChar)
        cmd.Parameters.Item("@depoid").Value = _Depo_No
        cmd.Parameters.Item("@barkod").Value = barkod

        Dim adp As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)

        Dim stokid As Integer = 0
        Dim stokad As String = ""
        Dim anagrup As String = ""
        Dim genelfiyat As Decimal = 0
        Dim depofiyat As Decimal = 0
        Dim genelfiyat2 As Decimal = 0
        Dim depofiyat2 As Decimal = 0
        Dim kullanilacakfiyat As Decimal = 0
        Dim kullanilacakfiyat2 As Decimal = 0
        Dim etikettipi As Integer = 0
        Dim birim1ad As String = ""
        Dim agirlik As Decimal = 0

        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1

                stokid = dt.Rows(i)("stokid")
                barkod = dt.Rows(i)("barkod")
                stokad = dt.Rows(i)("stokad")
                If Not dt.Rows(i)("genelfiyat1") Is DBNull.Value Then genelfiyat = dt.Rows(i)("genelfiyat1") Else genelfiyat = 0
                If Not dt.Rows(i)("depofiyat1") Is DBNull.Value Then depofiyat = dt.Rows(i)("depofiyat1") Else depofiyat = 0
                If Not dt.Rows(i)("barkodtipi") Is DBNull.Value Then etikettipi = dt.Rows(i)("barkodtipi") Else etikettipi = 0
                If Not dt.Rows(i)("birim1ad") Is DBNull.Value Then birim1ad = dt.Rows(i)("birim1ad") Else etikettipi = 0
                If Not dt.Rows(i)("birim1agirlik") Is DBNull.Value Then agirlik = dt.Rows(i)("birim1agirlik") Else agirlik = 0

                If Not dt.Rows(i)("genelfiyat2") Is DBNull.Value Then genelfiyat2 = dt.Rows(i)("genelfiyat2") Else genelfiyat2 = 0
                If Not dt.Rows(i)("depofiyat2") Is DBNull.Value Then depofiyat2 = dt.Rows(i)("depofiyat2") Else depofiyat2 = 0
                If Not dt.Rows(i)("anagrupkod") Is DBNull.Value Then anagrup = dt.Rows(i)("anagrupkod") Else anagrup = ""

                If depofiyat <= 0 Then
                    kullanilacakfiyat = genelfiyat
                Else
                    kullanilacakfiyat = depofiyat
                End If

                If depofiyat2 <= 0 Then
                    kullanilacakfiyat2 = genelfiyat2
                Else
                    kullanilacakfiyat2 = depofiyat2
                End If

                C1FlexGrid1(rw, 0) = True
                C1FlexGrid1(rw, 1) = barkod
                C1FlexGrid1(rw, 2) = stokad
                C1FlexGrid1(rw, 3) = kullanilacakfiyat
                C1FlexGrid1(rw, 7) = kullanilacakfiyat2
                C1FlexGrid1(rw, 4) = adet
                C1FlexGrid1(rw, 5) = etikettipi
                C1FlexGrid1(rw, 6) = birim1ad
                C1FlexGrid1(rw, 8) = agirlik
                C1FlexGrid1(rw, 9) = stokid
                C1FlexGrid1(rw, 10) = anagrup

                rw += 1
                C1FlexGrid1.Rows.Add()
                C1FlexGrid1.Refresh()
                Return True
            Next
        Else
            If mesajVerme = False Then
                MsgBox(barkod & "  Barkod Bulunamadı !")
            End If
            Return False
        End If
    End Function

    Private Sub Dosyaal(ByVal dosya As String)
        Try
            Dim sr As New StreamReader(dosya)
            Dim line As String = ""
            Dim strl() As String
            Dim barkod As String = ""
            Dim adet As Integer = 1
            Dim rw As Integer = C1FlexGrid1.Rows.Count - 1
            Dim sw As New StreamWriter("bulunamayanlar.txt", False)
            Dim bulunamayanvar As Boolean = False
            While sr.Peek > -1
                line = sr.ReadLine
                strl = line.Split(";")
                barkod = strl(0)
                adet = strl(1)
                If barkod <> "" And adet >= 1 Then
                    If Grideyaz(barkod, adet, rw, True) = True Then
                        rw += 1
                    Else
                        bulunamayanvar = True
                        sw.WriteLine(barkod)
                    End If
                End If
            End While
            sr.Close()
            cnn.Close()
            sw.Close()

            Process.Start("bulunamayanlar.txt")


        Catch ex As Exception
            MsgBox("Dosya Formatı barkod;miktar şeklinde olmalıdır !" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub C1FlexGrid1_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles C1FlexGrid1.AfterEdit
        If e.Col = 1 Then
            Dim barkod As String = C1FlexGrid1(e.Row, 1)
            Grideyaz(barkod, 1, C1FlexGrid1.Rows.Count - 1)
            cnn.Close()
        ElseIf e.Col = 2 Then
            Dim stokad As String = C1FlexGrid1(e.Row, 2)
            StokBul(stokad)
            cnn.Close()
        End If
        CalcRow()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        SaveFileDialog1.FileName = "Etiket.xls"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
            C1FlexGrid1.SaveExcel(SaveFileDialog1.FileName, "Etiketler", C1.Win.C1FlexGrid.FileFlags.AsDisplayed)
            Windows.Forms.Cursor.Current = Windows.Forms.Cursors.Default
            MsgBox("işlem tamamlandı")
        End If
    End Sub

    Private Sub C1FlexGrid1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles C1FlexGrid1.Click

    End Sub

    Private Sub C1FlexGrid2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles C1FlexGrid2.Click

    End Sub

    Private Sub C1FlexGrid2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles C1FlexGrid2.DoubleClick
        Dim r1 As Integer = C1FlexGrid2.Selection.r1
        Dim r2 As Integer = C1FlexGrid2.Selection.r2

        Dim rw As Integer = C1FlexGrid1.Rows.Count - 1
        For i As Integer = r1 To r2
            C1FlexGrid1(rw, 0) = True
            C1FlexGrid1(rw, 1) = C1FlexGrid2(i, 0)
            C1FlexGrid1(rw, 2) = C1FlexGrid2(i, 1)
            C1FlexGrid1(rw, 3) = C1FlexGrid2(i, 2)
            C1FlexGrid1(rw, 4) = 1
            C1FlexGrid1(rw, 5) = C1FlexGrid2(i, 4)
            C1FlexGrid1(rw, 6) = C1FlexGrid2(i, 3)
            C1FlexGrid1(rw, 7) = C1FlexGrid2(i, 6)
            C1FlexGrid1(rw, 8) = C1FlexGrid2(i, 7)
            C1FlexGrid1(rw, 9) = C1FlexGrid2(i, 8)
            C1FlexGrid1(rw, 10) = C1FlexGrid2(i, 9)
            rw += 1
            C1FlexGrid1.Rows.Add()
        Next
        TabControl1.SelectedIndex = 0
        CalcRow()
    End Sub

    Private Sub C1FlexGrid2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles C1FlexGrid2.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            Dim r1 As Integer = C1FlexGrid2.Selection.r1
            Dim r2 As Integer = C1FlexGrid2.Selection.r2

            Dim rw As Integer = C1FlexGrid1.Rows.Count - 1
            For i As Integer = r1 To r2
                C1FlexGrid1(rw, 0) = True
                C1FlexGrid1(rw, 1) = C1FlexGrid2(i, 0)
                C1FlexGrid1(rw, 2) = C1FlexGrid2(i, 1)
                C1FlexGrid1(rw, 3) = C1FlexGrid2(i, 2)
                C1FlexGrid1(rw, 4) = 1
                C1FlexGrid1(rw, 5) = C1FlexGrid2(i, 4)
                C1FlexGrid1(rw, 6) = C1FlexGrid2(i, 3)
                C1FlexGrid1(rw, 7) = C1FlexGrid2(i, 6)
                C1FlexGrid1(rw, 8) = C1FlexGrid2(i, 7)
                C1FlexGrid1(rw, 9) = C1FlexGrid2(i, 8)
                C1FlexGrid1(rw, 10) = C1FlexGrid2(i, 9)
                rw += 1
                C1FlexGrid1.Rows.Add()

            Next
            TabControl1.SelectedIndex = 0
            CalcRow()
        End If
    End Sub

    Private Sub TümünüİşaretleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TümünüİşaretleToolStripMenuItem.Click
        For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
            C1FlexGrid1(i, 0) = True
        Next
    End Sub

    Private Sub TümünüKaldırToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TümünüKaldırToolStripMenuItem.Click
        For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
            C1FlexGrid1(i, 0) = False
        Next
    End Sub

    Private Sub İkinciFiyatlarSeçToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles İkinciFiyatlarSeçToolStripMenuItem.Click
        For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
            If C1FlexGrid1(i, 7) > 0 Then
                C1FlexGrid1(i, 0) = True
            Else
                C1FlexGrid1(i, 0) = False
            End If
        Next
    End Sub

    Private Sub BirinciFiyatlarıSeçToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BirinciFiyatlarıSeçToolStripMenuItem.Click
        For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
            If C1FlexGrid1(i, 3) > 0 And C1FlexGrid1(i, 7) <= 0 Then
                C1FlexGrid1(i, 0) = True
            Else
                C1FlexGrid1(i, 0) = False
            End If
        Next
    End Sub

    Private Function CalcRow() As Integer
        Dim sonuc As Integer = 0
        For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
            If C1FlexGrid1.Rows(i)(0) = True And C1FlexGrid1.Rows(i)(4) > 0 Then
                sonuc += C1FlexGrid1.Rows(i)(4)
            End If
        Next
        Label1.Text = "Basılacak Etiket Miktarı :" & sonuc
        Return sonuc
    End Function


    Private Sub cmb_etikettip_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_etikettip.SelectedIndexChanged
        If cmb_etikettip.SelectedIndex > -1 Then
            EtiketYolDegistir()
        End If
    End Sub

    Private Sub C1FlexGrid1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles C1FlexGrid1.KeyDown
        If e.KeyCode = Windows.Forms.Keys.F5 Then
            Yazdir()
        End If
    End Sub

    Private m_nFirstCharOnPage As Integer
    Private RichQ As New System.Collections.Generic.Queue(Of RichTextBoxEx)
    Private RichTextEdit As RichTextBoxEx

    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        If RichQ.Count > 0 Then
            RichTextEdit = RichQ.Dequeue
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        If Not RichTextEdit Is Nothing Then
            ' To print the boundaries of the current page margins
            ' uncomment the next line:
            e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, e.MarginBounds)

            ' make the RichTextBoxEx calculate and render as much text as will
            ' fit on the page and remember the last character printed for the
            ' beginning of the next page
            m_nFirstCharOnPage = RichTextEdit.FormatRange(False, _
                                                    e, _
                                                    m_nFirstCharOnPage, _
                                                    RichTextEdit.TextLength)

            ' check if there are more pages to print
            If (m_nFirstCharOnPage < RichTextEdit.TextLength) Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        End If
    End Sub

    Private Sub btnKaydet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKaydet.Click
        Dim cn As New OleDbConnection(Cnn_String)
        cn.Open()
        Dim cmd As New OleDbCommand
        Dim cmd2 As New OleDbCommand
        Dim cmd3 As New OleDbCommand
        cmd.Connection = cn
        cmd2.Connection = cn
        cmd3.Connection = cn

        cmd.CommandText = String.Format("update TBL_STOK set genelfiyat1=@fiyat1,genelfiyat2=@fiyat2,tarih='{0}' where stokid =@id", Now.Day & "/" & Now.Month & "/" & Now.Year)
        cmd.Parameters.Add("@fiyat1", OleDbType.Double)
        cmd.Parameters.Add("@fiyat2", OleDbType.Double)
        cmd.Parameters.Add("@id", OleDbType.Integer)

        cmd2.CommandText = "update TBL_STOKDEPO set depofiyat1=@fiyat1,depofiyat2=@fiyat2 where stokid =@id"
        cmd2.Parameters.Add("@fiyat1", OleDbType.Double)
        cmd2.Parameters.Add("@fiyat2", OleDbType.Double)
        cmd2.Parameters.Add("@id", OleDbType.Integer)

        'cmd3.CommandText = "insert into TBL_HISTORY (Tarih,Fiyat,stokid) values (@tarih,@fiyat,@id)"
        'cmd3.Parameters.Add("@fiyat", OleDbType.Currency)
        'cmd3.Parameters.Add("@tarih", OleDbType.Date)
        'cmd3.Parameters.Add("@id", OleDbType.Integer)

        Dim anagrupkodu As String
        For i As Integer = 1 To C1FlexGrid1.Rows.Count - 1
            If C1FlexGrid1(i, 1) <> "" And C1FlexGrid1(i, 2) <> "" And C1FlexGrid1(i, 0) = True And C1FlexGrid1(i, 3) > 0 Then

                anagrupkodu = C1FlexGrid1(i, 10)

                If fiyatGruplari.IndexOf(anagrupkodu) > -1 Then
                    cmd.Parameters(0).Value = C1FlexGrid1(i, 3)
                    cmd.Parameters(1).Value = C1FlexGrid1(i, 7)
                    cmd.Parameters(2).Value = C1FlexGrid1(i, 9)
                    cmd.ExecuteNonQuery()

                    cmd2.Parameters(0).Value = C1FlexGrid1(i, 3)
                    cmd2.Parameters(1).Value = C1FlexGrid1(i, 7)
                    cmd2.Parameters(2).Value = C1FlexGrid1(i, 9)
                    cmd2.ExecuteNonQuery()


                    cmd3.CommandText = String.Format("insert into TBL_HISTORY (Tarih,Fiyat,stokid) values ('{0}','{1}',{2})", Now.Day & "/" & Now.Month & "/" & Now.Year, C1FlexGrid1(i, 3).ToString(), C1FlexGrid1(i, 9))
                    cmd3.ExecuteNonQuery()
                End If

            End If
        Next
        cn.Close()

        MsgBox("Kaydedildi !")
    End Sub

    Private Sub EtiketBasımEvrağındanToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EtiketBasımEvrağındanToolStripMenuItem.Click
        Try
            UzakServis.Url = Servis_Url
            Dim ds As DataSet = UzakServis.FillDataSet(Sql_Server, Database_Name, String.Format("SELECT Etkb_Barkodu, Etkb_BasilacakMiktar FROM ETIKETBAS WHERE (Etkb_evrakno_seri = N'{0}') AND (Etkb_evrakno_sira =(SELECT ISNULL(MAX(Etkb_evrakno_sira), 0) AS EXP1 FROM ETIKETBAS AS ETIKETBAS_1 WHERE (Etkb_evrakno_seri = N'{0}')))", "ETK" + Depo_Kod))
            If Not ds Is Nothing Then
                If ds.Tables.Count > 0 Then
                    Dim rw As Integer = C1FlexGrid1.Rows.Count - 1
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        Grideyaz(ds.Tables(0).Rows(i)(0).ToString(), ds.Tables(0).Rows(i)(1), rw, True)
                        rw += 1
                    Next
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
